/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * TrainingBook
 * Copyright (C) Arnaud Duval 2010 <arnaud.duval@gmail.com>
 * 
 * TrainingBook is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * TrainingBook is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "preferences-dialog.hpp"

//Incluude of iostream should be removed after debug
#include <iostream>


///@brief Constructor
///@param parent Pointer to the parent window
///@param actTypes Activity typess
///@param DosmTilesURL URL for OSM tiles repository
PreferencesDialog::PreferencesDialog(Gtk::Window *parent,
                                     ActivityTypes actTypes,
                                     std::string DosmTilesURL)
{
    activityTypes = actTypes;
    osmTilesURL = DosmTilesURL;
    //Set the border width of the window
    set_border_width(0);
    set_title("Mettre un titre ici");
    

    //Ajout de boutons
    add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
    add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);

    activityTypesLabel.set_text("Types d'activités :");
    FillActivityTypesTextBuffer();
    activityTypesTextView.set_buffer(textBuffer);

    activityTypesVBox.pack_start(activityTypesLabel, Gtk::PACK_SHRINK);
    activityTypesVBox.pack_start(activityTypesTextView, Gtk::PACK_EXPAND_WIDGET);


    osmTilesURLLabel.set_text("URL du dépôt de tuile Openstreetmap");
    osmTilesURLEntry.set_text(osmTilesURL);
    
    osmTilesURLVBox.pack_start(osmTilesURLLabel, Gtk::PACK_SHRINK);
    osmTilesURLVBox.pack_start(osmTilesURLEntry, Gtk::PACK_EXPAND_WIDGET);
    

    get_vbox()->pack_start(activityTypesVBox, Gtk::PACK_EXPAND_WIDGET);
    get_vbox()->pack_start(osmTilesURLVBox, Gtk::PACK_EXPAND_WIDGET);

    show_all();
}

///@brief Destructor
PreferencesDialog::~PreferencesDialog()
{
  
}

///@brief Return activity types
ActivityTypes PreferencesDialog::GetActivityTypes()
{
    std::string strActivityTypes = textBuffer->get_text();
    activityTypes.Clear();

    size_t found = strActivityTypes.find("\n");
    while(found != std::string::npos)
    {
        std::string toAdd = strActivityTypes.substr(0, found);
        strActivityTypes.erase(0,found+1);
        found = strActivityTypes.find("\n");
        activityTypes.Add(toAdd);
    }
    activityTypes.Add(strActivityTypes);

    return activityTypes;
}

///@brief Return OSM tiles repository
std::string PreferencesDialog::GetOsmTilesRepository()
{
    osmTilesURL = osmTilesURLEntry.get_text();
    return osmTilesURL;
}

///@brief Fill the activity types text buffer with the list of activity types
void PreferencesDialog::FillActivityTypesTextBuffer()
{
    std::string content;
    textBuffer = Gtk::TextBuffer::create();
    for(unsigned int i = 0 ; i < activityTypes.NbTypes() ; i++)
    {
        content += activityTypes.Type(i);
        if(i != activityTypes.NbTypes() - 1)
            content += "\n";
    }
    textBuffer->set_text(content);
}


